View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/expression/MethodExpression.java,v 1.2 2002/07/01 18:56:26 rdonkin Exp $ 3 * $Revision: 1.2 $ 4 * $Date: 2002/07/01 18:56:26 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>;. 59 * 60 * $Id: MethodExpression.java,v 1.2 2002/07/01 18:56:26 rdonkin Exp $ 61 */ 62 package org.apache.commons.betwixt.expression; 63 64 import java.lang.reflect.Method; 65 66 /*** <p><code>MethodExpression</code> evaluates a method on the current bean context.</p> 67 * 68 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 69 * @version $Revision: 1.2 $ 70 */ 71 public class MethodExpression implements Expression { 72 73 /*** null arguments */ 74 protected static Object[] NULL_ARGUMENTS; 75 protected static Class[] NULL_CLASSES; 76 77 /*** The method to call on the bean */ 78 private Method method; 79 80 /*** Base constructor */ 81 public MethodExpression() { 82 } 83 84 /*** Convenience constructor sets method property */ 85 public MethodExpression(Method method) { 86 this.method = method; 87 } 88 89 /*** Evaluate by calling the read method on the current bean */ 90 public Object evaluate(Context context) { 91 Object bean = context.getBean(); 92 if ( bean != null ) { 93 Object[] arguments = getArguments(); 94 try { 95 return method.invoke( bean, arguments ); 96 } 97 catch (IllegalAccessException e) { 98 // lets try use another method with the same name 99 try { 100 Class type = bean.getClass(); 101 Method alternate = findAlternateMethod( type, method ); 102 if ( alternate != null ) { 103 return alternate.invoke( bean, arguments ); 104 } 105 } 106 catch (Exception e2) { 107 handleException(context, e2); 108 } 109 } 110 catch (Exception e) { 111 handleException(context, e); 112 } 113 } 114 return null; 115 } 116 117 public void update(Context context, String newValue) { 118 // do nothing 119 } 120 121 /*** Gets the constant value of this expression */ 122 public Method getMethod() { 123 return method; 124 } 125 126 /*** Sets the constant value of this expression */ 127 public void setMethod(Method method) { 128 this.method = method; 129 } 130 131 // Implementation methods 132 //------------------------------------------------------------------------- 133 134 /*** Allows derived objects to create arguments for the method call */ 135 protected Object[] getArguments() { 136 return NULL_ARGUMENTS; 137 } 138 139 /*** Tries to find an alternate method for the given type using interfaces 140 * which gets around the problem of inner classes, 141 * such as on Map.Entry implementations. 142 */ 143 protected Method findAlternateMethod( 144 Class type, 145 Method method ) 146 throws 147 NoSuchMethodException { 148 Class[] interfaces = type.getInterfaces(); 149 if ( interfaces != null ) { 150 String name = method.getName(); 151 for ( int i = 0, size = interfaces.length; i < size; i++ ) { 152 Class otherType = interfaces[i]; 153 Method alternate = otherType.getMethod( name, NULL_CLASSES ); 154 if ( alternate != null && alternate != method ) { 155 return alternate; 156 } 157 } 158 } 159 return null; 160 } 161 162 /*** 163 * <p> Log error to context's logger. </p> 164 * 165 * <p> Allows derived objects to handle exceptions differently. </p> 166 */ 167 protected void handleException(Context context, Exception e) { 168 // use the context's logger to log the problem 169 context.getLog().error("[MethodExpression] Cannot evaluate expression", e); 170 } 171 172 public String toString() { 173 return "MethodExpression [method=" + method + "]"; 174 } 175 }

This page was automatically generated by Maven